package com.cloudlibrary.service.impl;

import com.cloudlibrary.domain.Book;
import com.cloudlibrary.domain.Record;
import com.cloudlibrary.domain.User;
import com.cloudlibrary.entity.PageResult;

import com.cloudlibrary.mapper.RecordMapper;
import com.cloudlibrary.service.BookService;
import com.cloudlibrary.mapper.BookMapper;
import com.cloudlibrary.service.RecordService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
* @author FoxPrime
* @description 针对表【book】的数据库操作Service实现
* @createDate 2024-12-12 10:26:57
*/
@Service
public class BookServiceImpl implements BookService{
    @Autowired
    private   BookMapper bookMapper;
    @Autowired
    private RecordService recordService;

    @Override
    public PageResult selectBookById(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<Book> page = bookMapper.selectBookById();
        return new PageResult(page.getTotal(), page.getResult());
    }

    public Book findById(String id) {
        return bookMapper.findById(id);
    }
    @Override
    public Integer borrowBook(Book book) {
        Book b = this.findById(book.getId()+"");
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        book.setborrowtime(dateFormat.format(new Date()));
        book.setStatus("1");
        book.setUploadtime(b.getUploadtime());
        return bookMapper.editBook(book);
    }
    @Override
    public PageResult search(Book book, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<Book> page=bookMapper.searchBooks(book);
        return new PageResult(page.getTotal(), page.getResult());
    }
    @Override
    public Integer addBook(Book book){
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        book.setborrowtime(dateFormat.format(new Date()));
        return bookMapper.addBook(book);
    }
    @Override
    public Integer editBook(Book book){
        return bookMapper.editBook(book);
    }

    @Override
    public PageResult searchBorrowed(Book book, User user, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<Book> page;
        book.setBorrower(user.getName());
        if ("ADMIN".equals(user.getRole())) {
            page = bookMapper.selectBorrowed(book);
        }else {
            page = bookMapper.selectMyBorrowed(book);
        }
        return new PageResult(page.getTotal(), page.getResult());
    }
    @Override
    public boolean returnBook(String id,User user) {
        Book book = this.findById(id);
        boolean rb = book.getBorrower().equals(user.getName());
        if (rb){
            book.setStatus("2");
            bookMapper.editBook(book);
        }
        return rb;
    }
//    @Override
//    public Integer returnConfirm(String id){
//        Book book = this.findById(id);
//        book.setStatus("0");
//        book.setBorrower("");
//        book.setborrowtime("");
//        book.setreturntime("");
//        return bookMapper.editBook(book);
//    }
    @Override
    public Integer returnConfirm(String id){
        Book book = this.findById(id);
        Record record = this.setRecord(book);
        book.setStatus("0");
        book.setBorrower("");
        book.setborrowtime("");
        book.setreturntime("");
        Integer count = bookMapper.editBook(book);
        if (count ==1){
            return recordService.addRecord(record);
        }
        return 0;
    }
    private Record setRecord(Book book){
        Record record = new Record();
        record.setBookname(book.getName());
        record.setBookisbn(book.getIsbn());
        record.setBorrower(book.getBorrower());
        record.setBorrowtime(book.getborrowtime());
        DateFormat  dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        record.setRemandtime(dateFormat.format(new Date()));
        return record;
    }
}




